package com.nfc.lock.controller;

import com.nfc.lock.common.result.Result;
import com.nfc.lock.dto.request.RegionAddRequest;
import com.nfc.lock.entity.Region;
import com.nfc.lock.service.IRegionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 区域Controller
 */
@Tag(name = "区域管理")
@RestController
@RequestMapping("/api/v1/region")
@RequiredArgsConstructor
public class RegionController {

    private final IRegionService regionService;

    @Operation(summary = "获取区域树")
    @GetMapping("/tree")
    public Result<List<Region>> getRegionTree(
            @Parameter(description = "父区域ID，默认0") @RequestParam(required = false) Long parentId
    ) {
        return Result.success(regionService.getRegionTree(parentId));
    }

    @Operation(summary = "新增区域")
    @PostMapping("/add")
    public Result<Void> addRegion(@Valid @RequestBody RegionAddRequest request) {
        regionService.addRegion(request);
        return Result.success();
    }

    @Operation(summary = "更新区域")
    @PutMapping("/edit/{regionId}")
    public Result<Void> updateRegion(
            @Parameter(description = "区域ID") @PathVariable Long regionId,
            @Valid @RequestBody RegionAddRequest request
    ) {
        regionService.updateRegion(regionId, request);
        return Result.success();
    }

    @Operation(summary = "删除区域")
    @DeleteMapping("/delete/{regionId}")
    public Result<Void> deleteRegion(@Parameter(description = "区域ID") @PathVariable Long regionId) {
        regionService.deleteRegion(regionId);
        return Result.success();
    }

    @Operation(summary = "获取子区域列表")
    @GetMapping("/children")
    public Result<List<Region>> getChildrenRegions(
            @Parameter(description = "父区域ID") @RequestParam Long parentId
    ) {
        return Result.success(regionService.getChildrenRegions(parentId));
    }
}
